package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.en.DictCodeEnum;
import com.atguigu.en.HouseImageTypeEnum;
import com.atguigu.en.HouseStatusEnum;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    @DubboReference
    private HouseService houseService;

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private DictService dictService;

    @DubboReference
    private HouseImageService houseImageService;

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private HouseUserService houseUserService;

    //先进入房屋管理首页
    @PreAuthorize("hasAuthority('house.show')")
    @RequestMapping
    public String index(@RequestParam Map<String, Object> filters, Model model) {
        //1.查询房源的分页信息，并存储到请求域
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page", page);
        //2.将搜索条件存储到请求域
        model.addAttribute("filters", filters);
        //3.查询显示所有小区、户型、楼型、楼层、建筑结构、朝向、装修情况、房屋用途，并存储到请求域
        initData(model);

        return "house/index";
    }

    //显示新增页面
    @PreAuthorize("hasAuthority('house.create')")
    @GetMapping("/create")
    public String create(Model model) {
        initData(model);
        return "house/create";
    }

    //保存新增数据
    @PreAuthorize("hasAuthority('house.create')")
    @PostMapping("/save")
    public String save(House house, Model model) {
        //1房源表中有一个statues字段，这个字段的值在新增页面中没有传过来，而且这个字段的值又不能为null，所以在新增成员前
        //需要先给status赋值，表示房源是否已发布，0：未发布、1：已发布   通过枚举的方式来做会好一些
        //house.setStatus(0);
        house.setStatus(HouseStatusEnum.UNPUBLISHED.getCode());
        //2.调用业务层方法保存房源信息
        houseService.insert(house);
        //3.显示成功页面,需要HouseController 继承 BaseController
        return "common/successPage";
    }

    /**
     * 修改房源信息
     * 先要根根据id查询房源信息并进行回显
     * 再进行修改提交
     */
    @PreAuthorize("hasAuthority('house.edit')")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        //1.调用业务层方法根据id查询房源信息，并存储到请求域
        House house = houseService.getById(id);
        model.addAttribute("house", house);
        //2.初始化数据
        initData(model);
        return "house/edit";
    }

    @PreAuthorize("hasAuthority('house.edit')")
    @PostMapping("/update")
    public String update(House house) {
        //1.调用业务层方法修改房源信息
        houseService.update(house);
        //2.显示成功页面
        return "common/successPage";
    }

    //删除房源
    @PreAuthorize("hasAuthority('house.delete')")
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        //1.调用业务层的方法修改房源信息
        houseService.delete(id);
        //2.重定向到显示房源的首页
        return "redirect:/house";
    }


    //发布或取消发布
    @PreAuthorize("hasAuthority('house.publish')")
    @GetMapping("/publish/{id}/{status}")
    public String publishOrUnPublish(@PathVariable Long id,
                                     @PathVariable Integer status) {
        //1.创建House对象，并且设置House的id和status
        House house = new House();
        house.setId(id);
        house.setStatus(status);
        //2.调用业务层的方法修改房源信息
        houseService.update(house);
        //3.重定向访问房源的首页
        return "redirect:/house";
    }

    @PreAuthorize("hasAuthority('house.show')")
    @GetMapping("/{houseId}")
    public String detail(@PathVariable("houseId") Long houseId, Model model) {
        //1.查询房源信息存储到请求域
        House house = houseService.getById(houseId);
        model.addAttribute("house", house);
        //2.查询小区信息存储到请求域
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community", community);
        //3.房源图片列表
        List<HouseImage> houseImage1List = houseImageService.findHouseImageListByHouseId(houseId, HouseImageTypeEnum.HOUSE_SOURCE.getCode());
        model.addAttribute("houseImage1List", houseImage1List);
        //4.房产图片列表存储到请求域
        List<HouseImage> houseImage2List = houseImageService.findHouseImageListByHouseId(houseId, HouseImageTypeEnum.HOUSE_PROPERTY.getCode());
        model.addAttribute("houseImage2List", houseImage2List);
        //5.查房源的经纪人列表并存储到请求域
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(houseId);
        model.addAttribute("houseBrokerList", houseBrokerList);
        //6.查房东列表并存储到请求域
        List<HouseUser> houseUserList = houseUserService.findHouseUserListByHouseId(houseId);
        model.addAttribute("houseUserList", houseUserList);
        return "house/show";
    }


    //初始化数据
    private void initData(Model model) {
        //查询小区列表，存储到请求域
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList", communityList);
        List<Dict> houseTypeList = dictService.findListByParentDictCode(DictCodeEnum.HOUSETYPE.getCode());
        model.addAttribute("houseTypeList", houseTypeList);
        List<Dict> floorList = dictService.findListByParentDictCode(DictCodeEnum.FLOOR.getCode());
        model.addAttribute("floorList", floorList);
        List<Dict> buildStructureList = dictService.findListByParentDictCode(DictCodeEnum.BUILDSTRUCTURE.getCode());
        model.addAttribute("buildStructureList", buildStructureList);
        List<Dict> directionList = dictService.findListByParentDictCode(DictCodeEnum.DIRECTION.getCode());
        model.addAttribute("directionList", directionList);
        List<Dict> decorationList = dictService.findListByParentDictCode(DictCodeEnum.DECORATION.getCode());
        model.addAttribute("decorationList", decorationList);
        List<Dict> houseUseList = dictService.findListByParentDictCode(DictCodeEnum.HOUSEUSE.getCode());
        model.addAttribute("houseUseList", houseUseList);
    }
}
